<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>编辑</title>
    <link href="../css/register.css" rel="stylesheet">
    <script src="../js/jquery.js"></script>
</head>

<body>
<div class="container">

    <div class="card o-hidden border-0 shadow-lg my-5">
        <div class="card-body p-0">
            <!-- Nested Row within Card Body -->
            <div class="row">
                <div class="col-lg-7">
                    <div class="p-5">
                        <!--                        <div class="text-center">-->
                        <!--                            <h1 class="h4 text-gray-900 mb-4"></h1>-->
                        <!--                        </div>-->
                        <div class="user">
                            <div class="form-group">
                                <span style="color: red;font-size: 12px;">*</span>
                                学号/工号/账号:
                                <input type="text" class="form-control form-control-user" id="userid"
                                       name="register_id" readonly><span
                                    style="color: red;font-size: 12px;">ID不可修改</span>
                            </div>
                            <div class="form-group">
                                <span style="color: red;font-size: 12px;">*</span>
                                姓名:
                                <input type="text" class="form-control form-control-user" id="username"
                                       name="register_name">
                            </div>
                            <div class="form-group">
                                <span style="color: red;font-size: 12px;">*</span>
                                密码:
                                <input type="text" class="form-control form-control-user" id="password"
                                       name="register_pwd">
                            </div>
                            <div class="form-group">
                                <span style="color: red;font-size: 12px;">*</span>
                                性别:
                                <label>
                                    <input type="radio" name="sex" id="sex1" value="男" checked>男
                                </label>
                                &nbsp;&nbsp;&nbsp;&nbsp;
                                <label>
                                    <input type="radio" name="sex" id="sex2" value="女">女
                                </label>
                            </div>
                            <div class="form-group">
                                <span style="color: red;font-size: 12px;">*</span>
                                院系:
                                <label for="faculty"></label>
                                <select id="faculty">
                                    <option id="faculty1" selected>计算机科学与技术</option>
                                    <option id="faculty2">软件工程</option>
                                    <option id="faculty3">物理学院</option>
                                    <option id="faculty4">土木工程</option>
                                </select>
                            </div>
                            <div class="form-group">
                                <span style="color: red;font-size: 12px;">*</span>
                                职务:
                                <label for="user_priority"></label>
                                <select id="user_priority">
                                    <option id="priority1" value="0" selected>学生</option>
                                    <option id="priority2" value="1">教师</option>
                                    <option id="priority3" value="2">管理员</option>
                                </select>
                            </div>

                            <button type="button" class="btn btn-primary btn-user btn-block"
                                    onclick="update()">确认修改
                            </button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    $(function () {
        $.ajax({
            type: "post",
            url: "http://localhost:8080/user/getCurrentUser",
            success: function (result) {
                if (result.code != '200') {
                    alert("请先登录账号！");
                    window.location.href = "login.html";
                }
            }
        });
    });
    let data = {"userid": localStorage["id"]}
    $(function () {
        $.post('http://localhost:8080/user/findById', data, function (result) {
            let res = result.object;
            if (result.code == 200) {
                document.getElementById("userid").value = res.userid;
                document.getElementById("username").value = res.username;
                document.getElementById("password").value = res.password;
                if (res.sex == '女') {
                    $("#sex1").prop("checked", false);
                    $("#sex2").prop("checked", true);
                }
                if (res.priority == 1) {
                    $("#priority1").prop("selected", false);
                    $("#priority2").prop("selected", true);
                } else if (res.priority == 2) {
                    $("#priority1").prop("selected", false);
                    $("#priority3").prop("selected", true);
                }
                if (res.faculty == "软件工程") {
                    $("#faculty1").prop("selected", false);
                    $("#faculty2").prop("selected", true);
                } else if (res.faculty == "物理学院") {
                    $("#faculty1").prop("selected", false);
                    $("#faculty3").prop("selected", true);
                } else if (res.faculty == "土木工程") {
                    $("#faculty1").prop("selected", false);
                    $("#faculty4").prop("selected", true);
                }
            }
        });
    });

    function update() {
        let register_id = $("#userid").val();
        let register_name = $("#username").val();
        let register_pwd = $("#password").val();
        let register_sex = $("input[name='sex']:checked").val();
        let register_faculty = $("#faculty option:selected").text();
        let register_priority = $("#user_priority option:selected").val();

        let data = {
            "userid": register_id,
            "username": register_name,
            "password": register_pwd,
            "sex": register_sex,
            "faculty": register_faculty,
            "priority": register_priority
        };
        $.post('http://localhost:8080/user/updateUserInfo', data, function (result) {
            if (result.code == '200') {
                alert(result.msg);
                location.replace(location.href);
            } else {
                alert(result.msg);
            }
        });
    }

</script>
</body>
</html>